import config from './config';
import md5 from "./md5";
import url from "./url"
//定义局部你变量 请求的域名
// const domain="http://www.lchfang.com/api";
//http请求的类
export default class Http{
    //header头信息的封装
    setHeader(){
          //当前时间戳
          let time=Date.now();
          //头信息
          //...参数扩展  追加
          return {
              username:config.username,
              password:config.password,
              timestemp:time,
              sign:md5(config.username+config.password+config.token+time),
          }
    }
    //发起http请求，返回promise对象
    httpRequest({url,data={},method="GET",header={}}){
        //用户交互提示
        wx.showLoading({
            title:"正在玩命加载中...",
            mask:true
        });
        //解决header
        header={...this.setHeader(),...header}
        return new Promise((resolve ,reject)=>{
            wx.request({
                url:config.domain+url,
                data,
                method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
                header, // 设置请求的 header
                success: ret=>resolve(ret),
                fail:err=>reject(err),
                complete: ()=> {
                    //隐藏加载框
                    wx.hideLoading();
                }
            })
        })
    }
    //图片上传
       uploadFile({filePath,formData={ node: "renting"}}){
            return new Promise((resolve, reject)=>{
                wx.uploadFile({
                    url: config.domain+url.rentingupfile,
                    filePath,
                    name: 'file',
                    header:this.setHeader(), // 设置请求的 header
                    formData,                    
                    success: ret => resolve(ret)
                    // {
                    //   let json = JSON.parse(ret.data);
                    //   let card_img = this.data.userinfo.card_img + "#" + json.pic;
                    //   this.setData({
                    //     userinfo: { ...this.data.userinfo, card_img }
                    //   })
                    // },
                  })
            })
       }
}